package org.example.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import jakarta.annotation.Resource;
import org.example.entity.EMData;
import org.example.service.UserService;
import org.example.common.Result;
import org.example.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

@RestController
@RequestMapping("/api")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        Result info = userService.login(user);
        return Result.success(info);
    }

    @PostMapping("/register")
    public Result register(@RequestBody User userInfo) {

        User newUser = userService.addUser(userInfo);

        return Result.success(newUser);
    }

    @PostMapping("/checkUsername")
    // 检验用户名是否可用
    public Result checkUsername(@RequestBody String username) {
        Result user = userService.checkUsername(username);
        return Result.success(user);
    }


    @PostMapping("/checkEmail")
    // 检验邮箱是否可用
    public Result checkEmail(@RequestBody String email) {
        Result user = userService.checkEmail(email);
        return Result.success(user);
    }

    @PostMapping("/checkMobile")
    // 检验手机号码是否可用
    public Result checkMobile(@RequestBody String mobile) {
        Result user = userService.checkMobile(mobile);
        return Result.success(user);
    }

    @PostMapping("/useEmail")
    public Result useEmail(@RequestBody String email) {
        Result result = userService.useEmail(email);
         return Result.success(result);
    }

    @PostMapping("/useMobile")
    public Result useMobile(@RequestBody String mobile) {
        Result result = userService.useMobile(mobile);
        return Result.success(result);
    }

    @PostMapping("/sendEmailCode")
    public Result sendEmailCode(@RequestBody User user) {
        if (StrUtil.isBlank(user.getEmail())){
            return Result.error("email地址不能为空！");
        }
        Result result =  userService.sendEmailCode(user);
        return Result.success(result);
    }

    @PostMapping("/editPassword")
    public Result editPassword(@RequestBody User user) {
        String rule = "^[A-Za-z0-9]{8}$";
        Pattern pattern = Pattern.compile(rule);
        Matcher matcher = pattern.matcher(user.getPassword());
        if(!matcher.matches()) {
            return Result.error("密码长度最少8位,包含数字字母组合");
        } else {
            Result result = userService.editPassword(user);
            return Result.success(result);
        }
    }

    // 检查邮箱验证码
    @PostMapping("/checkEmailCode")
    public Result checkEmailCode(@RequestBody EMData emData) {
        if(emData.getCode() == null || emData.getCode().length() < 6) {
            return  Result.error("邮箱验证码格式错误!");
        }
        else {
            Result result = userService.checkEmailCode(emData);
            System.out.println(result);
            return Result.success(result);
        }
    }

    // 检查手机验证码
    @PostMapping("/checkMobileCode")
    public Result checkMobileCode(@RequestBody EMData emData) {
        if(emData.getCode() == null || emData.getCode().length() < 6) {
            return  Result.error("手机验证码格式错误!");
        }
        else {
            Result result = userService.checkMobileCode(emData);
            System.out.println(result);
            return Result.success(result);
        }
    }
}
